package org.nnxy.foodboot.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.nnxy.foodboot.entity.Recipe;
import org.nnxy.foodboot.entity.UserCollection;

/**
 * 用户收藏Mapper接口
 */
@Mapper
public interface UserCollectionMapper extends BaseMapper<UserCollection> {

    /**
     * 分页查询用户收藏的食谱
     *
     * @param page   分页参数
     * @param userId 用户ID
     * @return 分页结果
     */
    @Select("SELECT r.* FROM recipe r " +
            "INNER JOIN user_collection uc ON r.id = uc.recipe_id " +
            "WHERE uc.user_id = #{userId} AND r.del_flag = '0' " +
            "ORDER BY uc.create_time DESC")
    IPage<Recipe> selectUserCollectionRecipes(Page<Recipe> page, @Param("userId") Long userId);


    /**
     * 检查用户是否已收藏食谱
     *
     * @param userId   用户ID
     * @param recipeId 食谱ID
     * @return 收藏数量
     */
    @Select("SELECT COUNT(1) FROM user_collection " +
            "WHERE user_id = #{userId} AND recipe_id = #{recipeId}")
    Integer checkUserCollection(Long userId, Long recipeId);

}


 